package com.module.prms;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.log4j.Logger;

import base.util.Page;
import base.util.ResolveUnit;

import com.activityClient.client.ActivityThread;
import com.module.prms.manager.OperationManager;
import com.module.prms.vo.PrmsOperation;
import com.module.sys.ConstantUnit;
import com.webClient.client.SocketClient;
import com.webClient.service.WebClientService;

public class operationService {
	static SocketClient sClient = null;
	static WebClientService ss = null;
	static Logger logger = Logger.getLogger(operationService.class.getName());

	private String getJson(Map map) throws Exception {
		String sessionId = UUID.randomUUID().toString();
		map.put(ConstantUnit.STATUS_NAME, ConstantUnit.STATUS_SEND);
		map.put(ConstantUnit.SESSIONID_NAME, sessionId);
		map.put(ConstantUnit.B_ID, "prms");
		map.put(ConstantUnit.CN, "PrmsOperation");
		String json = ResolveUnit.getJsonStr(map);
		return json;
	}

	public Page menuoptlist(PrmsOperation opt, final Integer start,
			final Integer limit) throws Exception {
		Page page = new Page();
		page.setStart(start);
		page.setLimit(limit);
		OperationManager operationManager=OperationManager.getInstance();
		page=operationManager.list(page,opt);
		return page;
	}

	public PrmsOperation findbyid(String operationid) {
		try {
			PrmsOperation obj = new PrmsOperation();
			obj.setCoperationId(operationid);
			OperationManager operationManager=OperationManager.getInstance();
			PrmsOperation po=operationManager.findebyid(obj);
			return po;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public Map save(PrmsOperation obj) {
		try {
			OperationManager operationManager=OperationManager.getInstance();
			boolean isTrue =operationManager.add(obj);
			Map<String, Object> statusMap = new HashMap<String, Object>();
			statusMap.put("state", isTrue);
			if (isTrue == true) {
				statusMap.put("msg", "保存成功");
			} else {
				statusMap.put("msg", "保存失败");
			}
			return statusMap;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public Map remove(List<String> idArray) throws Exception {
		OperationManager operationManager=OperationManager.getInstance();
		boolean isTrue =operationManager.remove(idArray);
		Map<String, Object> statusMap = new HashMap<String, Object>();
		statusMap.put("state", isTrue);
		if (isTrue == true) {
			statusMap.put("msg", "删除成功");
		} else {
			statusMap.put("msg", "删除失败");
		}
		return statusMap;
	}

	private PrmsBaseBean returnValue(Map map) throws Exception {
		try {
			String sendmsg = getJson(map);
			ActivityThread tc = ActivityThread.getInstance();
			logger.info("prms发送的报文是：" + sendmsg);
			String receiveMessage = null;
			try {
				receiveMessage = tc.sendAndReceive(sendmsg, map.get("sid")
						.toString());
				logger.info("prms接收的报文是：" + receiveMessage);
				PrmsBaseBean beanUnit = ResolveUnit.resolve(receiveMessage,
						PrmsBaseBean.class);
				return beanUnit;
			} catch (Exception e) {
				throw e;
			}

		} catch (Exception e) {
			throw e;
		}

	}

}
